package com.zthz.douxing

import com.google.common.eventbus.Subscribe
import com.zthz.douxing.data.TripSegmentArrivedJob
import com.zthz.douxing.data.TripSegmentOnPositionJob
import com.zthz.douxing.event.UpdateTripSegmentStatusEvent
import com.zthz.douxing.utils.CommonUtil
import org.quartz.JobDataMap
import org.quartz.JobKey
import org.quartz.SimpleTrigger
import org.quartz.impl.matchers.GroupMatcher
import org.quartz.impl.triggers.SimpleTriggerImpl

import javax.annotation.PostConstruct

import static org.quartz.JobBuilder.newJob

class TripSegmentEventHandleService {

    def quartzScheduler
    def plantripService
    def eventBusService

    private static String GRROUP_ON_POSITION_JOB = "changeOnPositionStatus"
    private static String GRROUP_ARRIVED_JOB = "changeArrrivedSatuts"
    @PostConstruct
    def init() {
        eventBusService.register(this)
    }

    /**
     * 接受到任务后，将该任务添加到quartz中去。等待执行
     * @param event
     */
    @Subscribe
    def handleTripSegmentEvent(UpdateTripSegmentStatusEvent event) {
        if (!event.validate()) {
            //TODO 处理错误
            return
        }

        JobDataMap jobDataMap = new JobDataMap()
        jobDataMap.put("tripSegmentEvent", event)
        jobDataMap.put("plantripService", plantripService)

        if(event?.tripSegment?.plantrip?.personOwner?.id){

            String onPositionJobName = CommonUtil.getJobKey(GRROUP_ON_POSITION_JOB,event.tripSegment.plantrip.personOwner.id)
            String arrivedJobName = CommonUtil.getJobKey(GRROUP_ARRIVED_JOB,event.tripSegment.plantrip.personOwner.id)

            String onPostionTriggerName = CommonUtil.getJobKey("onPositionTrigger",event.tripSegment?.startTime?.format("yyyyMMddHHmm"))
            String arrivedTriggerName = CommonUtil.getJobKey("arrivedTrigger",event.tripSegment?.arriveTime?.format("yyyyMMddHHmm"))

            if(!onPositionJobName || !arrivedJobName ||!onPostionTriggerName ||!arrivedTriggerName )
                return

            def onPositionJob = newJob(TripSegmentOnPositionJob)
                    .withIdentity(onPositionJobName, GRROUP_ON_POSITION_JOB)
                    .usingJobData(jobDataMap).build()
            def arrivedJob = newJob(TripSegmentArrivedJob)
                    .withIdentity(arrivedJobName,GRROUP_ARRIVED_JOB)
                    .usingJobData(jobDataMap).build()

            SimpleTrigger onPositionTrigger = new SimpleTriggerImpl(
                    onPostionTriggerName, event.tripSegment?.startTime, null, 0, 0L)
            SimpleTrigger arrivedTrigger = new SimpleTriggerImpl(
                    arrivedTriggerName, event.tripSegment?.arriveTime, null, 0, 0L)

            quartzScheduler.deleteJob(new JobKey(onPositionJobName,GRROUP_ON_POSITION_JOB))
            quartzScheduler.deleteJob(new JobKey(arrivedJobName,GRROUP_ARRIVED_JOB))
            quartzScheduler.scheduleJob(onPositionJob, onPositionTrigger)
            quartzScheduler.scheduleJob(arrivedJob, arrivedTrigger)
        }



    }

    def stopJob(TripSegment tripSegment) {
        Set<JobKey> jobKeys = quartzScheduler.getJobKeys(GroupMatcher.anyJobGroup())
        for(JobKey jobKey in jobKeys){
            if((jobKey.getGroup().equals(GRROUP_ON_POSITION_JOB) && jobKey.getName().equals(tripSegment?.startTime?.format("yyyyMMddHHmm")))
            || (jobKey.getGroup().equals(GRROUP_ARRIVED_JOB) && jobKey.getName().equals(tripSegment?.arriveTime?.format("yyyyMMddHHmm"))))
                quartzScheduler.deleteJob(jobKey)
        }
    }

    @Override
    public String toString() {
        return com.google.common.base.Objects.toStringHelper(this)
                .add("quartzScheduler", quartzScheduler)
                .toString();
    }
}